<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库状态" prop="inspcType">
        <el-select v-model="queryParams.inspcType" placeholder="请选择入库状态" clearable>
          <el-option v-for="dict in dict.type.mes_order_arrival" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:buyinhou:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:buyinhou:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:buyinhou:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:buyinhou:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buyinhouList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="采购订单编号" align="center" prop="buyOrderCode">

      </el-table-column>
      <el-table-column label="采购订单名称" align="center" prop="orderName">

      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="mtrCode">

      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="mtrName">

      </el-table-column>
      <el-table-column label="数量" align="center" prop="buyQuantity">

      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="inspcType">
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.inspcType)" effect="dark">
            <dict-tag :options="dict.type.mes_inhou_status" :value="scope.row.inspcType" />
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier">

      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.inspcType" size="mini" type="text" icon="el-icon-edit" @click="ruku(scope.row)"
            v-hasPermi="['mes:buyinhou:edit']">入库</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:buyinhou:edit']">修改</el-button> -->
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:buyinhou:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <transition name="el-zoom-in-center">


      <el-dialog id="rukudialog" title="选择仓库" :visible.sync="rukuopen" v-if="rukuopen" width="500px" append-to-body>
        <label>入库仓库：</label>
        <el-select v-model="houId" filterable placeholder="请选择" style="width: 30%;" @change="chooseHou">
          <el-option v-for="item in houoptions.filter(item => { return item.houType == 0 })" :key="item.houId"
            :label="item.houName" :value="item.houId">
          </el-option>
        </el-select>
        <el-select v-if="houId" v-model="locatorId" filterable placeholder="请选择" style="width: 30%;margin-left: 20px;">
          <el-option v-for="item in locator" :key="item"
            :label="item.code" :value="item.id">
          </el-option>
        </el-select>

        <transition name="el-zoom-in-top">
          <div v-show="locatorId" v-if="locatorId">
            <el-card id="rukucard"
              style="margin-top: 10px;border-radius: 15px;background-color: rgba(132, 200, 205, 0.557);" shadow="hover">
              <label style="display: inline-block;margin: auto;margin-top: 10px;">库位容量: <span>{{ locator.find(item=>item.id==locatorId).remainCapacity+'/'+locator.find(item=>item.id==locatorId).maxiCapacity}} </span></label>
              <el-progress :text-inside="true" :color="customColorMethod(calculateInventory())" :stroke-width="20"
                :percentage="calculateInventory()" status="exception"></el-progress>
            </el-card>

          </div>

        </transition>
        <el-divider content-position="left">入库产品</el-divider>
        <el-card id="rukucard" style=" margin-top: 10px;border-radius: 15px;background-color: rgba(75, 193, 214, 0.557);"
          shadow="always">
          <div slot="header" class="clearfix">
            <h5 style="display: inline-block;">物料编码: <h3 style="display: inline-block;font-style:italic;">{{
              nowrowData.mtrCode }}</h3>
            </h5>
          </div>
          <div style="display: inline-block;">
            <div style="background-color: rgba(240, 248, 255, 0.621);padding: 4px;border-radius: 7px;margin-bottom: 5px;">
              <span
                style="background-color: rgba(178, 170, 161, 0.502);padding: 5px;border-radius: 7px;font-size: 15px;">物料名称:</span>
              <label style="font-size: 15px; font-weight: unset;"> {{ nowrowData.mtrName }}</label>
            </div>
            <div style="background-color: rgba(240, 248, 255, 0.621);padding: 4px;border-radius: 7px;margin-bottom: 5px;">
              <span
                style="background-color: rgba(178, 170, 161, 0.502);padding: 5px;border-radius: 7px;font-size: 15px;">入库数量:</span>
              <label style="font-size: 15px; font-weight: unset;"> {{ nowrowData.buyQuantity }}</label>
            </div>
            <div style="background-color: rgba(240, 248, 255, 0.621);padding: 4px;border-radius: 7px;margin-bottom: 5px;">
              <span
                style="background-color: rgba(178, 170, 161, 0.502);padding: 5px;border-radius: 7px;font-size: 15px;">供应商:</span>
              <label style="font-size: 15px; font-weight: unset;"> {{ nowrowData.supplier }}</label>
            </div>
          </div>

          <img style="display: inline-block;float: right;" src="@/assets/images/mtr.png" />
        </el-card>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="inHou">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </transition>


    <!-- 添加或修改采购入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入库状态" prop="inspcType">
          <el-select v-model="form.inspcType" placeholder="请选择入库状态">
            <el-option v-for="dict in dict.type.mes_inhou_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuyinhou, getBuyinhou, delBuyinhou, addBuyinhou, updateBuyinhou, inHou } from "@/api/mes/buyinhou";
import { listHouinfo } from "@/api/mes/houinfo"
import { getLocatorByHouId } from "@/api/mes/locator"
export default {
  name: "Buyinhou",
  dicts: ['mes_inhou_status'],
  data() {
    return {
      locator:[],//库位下拉数据
      //当前选择记录数据
      nowrowData: [],
      //仓库下拉框数据
      houoptions: [],
      //选择的仓库
      houId: "",
      //选择的库位
      locatorId:"",
      //入库对话框
      rukuopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购入库表格数据
      buyinhouList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inspcType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //容量计算
    capacityCalculation(){
      let nowLocator= this.locator.find(item=>item.id==this.locatorId)
      return nowLocator.remainCapacity+'/'+nowLocator.maxiCapacity

    },
    chooseHou(e){
      getLocatorByHouId({houId:e}).then(res=>{
        this.locator=res.data
      })
    },
    //标签类型判断
    tagType(status) {
      switch (status) {
        case 0:
          return ""
        case 1:
          return "success"
      }
    },
    //计算库存比例
    calculateInventory() {
      let nowLocator=this.locator.find(item=>item.id==this.locatorId)
      return parseFloat((1-nowLocator.remainCapacity/nowLocator.maxiCapacity).toFixed(2))
    },

    //进度条颜色控制
    customColorMethod(data) {
      if (data < 30) {
        return '#f56c6c'
      } else if (data < 100) {
        return '#e6a23c';
      } else {
        return '#67c23a';
      }
    },
    //入库确认
    inHou() {
      if (this.locatorId == "") {
        this.$modal.msgError("请选择入库库位");
      } else {
      inHou({ id: this.nowrowData.id, houId: this.houId ,locatorInfoId:this.locatorId}).then(res => {
        this.rukuopen = false
        this.$modal.msgSuccess("入库成功！");
        this.getList()
      })
    }
    },
    //入库按钮
    ruku(row) {
      this.nowrowData = row
      this.houId = ""
      console.log("🚀 ~ file: index.vue:180 ~ ruku ~ this.nowrowData:", this.nowrowData)
      listHouinfo().then(res => {
        this.houoptions = res.rows
        this.rukuopen = true
      })
    },
    /** 查询采购入库列表 */
    getList() {
      this.loading = true;
      listBuyinhou(this.queryParams).then(response => {
        this.buyinhouList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.rukuopen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        buyId: null,
        inspcType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuyinhou(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购入库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuyinhou(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuyinhou(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采购入库编号为"' + ids + '"的数据项？').then(function () {
        return delBuyinhou(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/buyinhou/export', {
        ...this.queryParams
      }, `buyinhou_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
#rukucard>>>.el-card__header {
  background-color: rgb(214, 210, 254);
  padding-bottom: 0px;
}

#rukucard {
  box-shadow: 3px 3px 5px rgba(128, 128, 128, 0.699);
}

#rukudialog>>>.el-dialog {
  border-radius: 10px;
}</style>
